﻿using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Extensions
{
    /// <summary>
    /// Signalr通信
    /// </summary>
    public class SignalRHub : Hub
    {
        static List<User> users = new List<User>() {
            new User{ID=1,Name="狗蛋" },
            new User{ID=2,Name="二狗" },
            new User{ID=3,Name="富贵" },
            new User{ID=4,Name="大侠" },
            new User{ID=5,Name="小弟" },
        };
        static int index = 0;
        public SignalRHub()
        {
        }

        /// <summary>
        /// 连接
        /// </summary>
        /// <returns></returns>
        public override Task OnConnectedAsync()
        {
            users[index].ConnectionId = Context.ConnectionId;
            Clients.All.SendAsync("ReceiveMessage", "系统[" + DateTime.Now.ToLongTimeString() + "]", $"欢迎「{users[index].Name}」进入群聊！");
            index++;
            if (index >= users.Count)
            {
                index = 0;
            }
            return base.OnConnectedAsync();
        }

        /// <summary>
        /// 退出连接
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public override Task OnDisconnectedAsync(Exception exception)
        {
            foreach (var item in users)
            {
                if (item.ConnectionId != null)
                {
                    if (item.ConnectionId.Contains(Context.ConnectionId))
                    {
                        item.ConnectionId = null;
                    }
                }

            }
            return base.OnDisconnectedAsync(exception);
        }
        /// <summary>
        /// 获取在线人
        /// </summary>
        /// <returns></returns>
        public List<User> GetOnline()
        {
            return users.Where(n => n.ConnectionId != null).ToList();
        }

        /// <summary>
        /// 发送群聊消息
        /// </summary>
        /// <param name="user">用户名</param>
        /// <param name="message">消息内容</param>
        /// <returns></returns>
        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user + "[" + DateTime.Now.ToLongTimeString() + "]", message);
        }
        /// <summary>
        /// 发送单聊消息
        /// </summary>
        /// <param name="conid">用户单聊标识</param>
        /// <param name="user">用户名</param>
        /// <param name="message">消息内容</param>
        /// <returns></returns>
        public async Task SendMessageToClient(string conid, string user, string message)
        {
            await Clients.Clients(conid, Context.ConnectionId).SendAsync("ReceiveMessageToClient", user + "[" + DateTime.Now.ToLongTimeString() + "]", message);
        }
        public async Task SendMessageToUser(string conid, string user, string message)
        {
            await Clients.User(conid).SendAsync("ReceiveMessageToUser", user + ":" + DateTime.Now.ToLongTimeString() + "]", message);
        }

    }

    public class User
    {
        public int ID { get; set; }
        public string ConnectionId { get; set; }
        public string Name { get; set; }
    }
}
